package com.example.dllo.keepdemo.person;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.dynamic.DynamicDetailActivity;
import com.example.dllo.keepdemo.person.bean.PersonDynamicBase;
import com.example.dllo.keepdemo.person.bean.PersonMessageBean;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

/**
 * Created by dllo on 16/1/16.
 */
public class PersonMessageActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView;
    private PersonDynamicListViewAdapter adapter;
    private PersonMessageBean personMessageBean;
    private PersonDynamicBase personDynamicBase;
    private RoundImageView imageheader;
    private TextView tvDynamic,tvCare,tvfans,tvContent,tvTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personmessage);
        listView = (ListView) findViewById(R.id.listview_activity_personmessage);
        toolbar = (Toolbar) findViewById(R.id.toolbar_activity_personmessage);
        tvTitle = (TextView) findViewById(R.id.tv_activity_personmessage_username);

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.actionlastbtn);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonMessageActivity.this.finish();
            }
        });


        View view = LayoutInflater.from(this).inflate(R.layout.header_listview_personmessage,null);
        listView.addHeaderView(view);
        imageheader = (RoundImageView) view.findViewById(R.id.img_header_activity_personmessage);
        tvDynamic = (TextView) view.findViewById(R.id.tv_header_activity_personmessage_dynamic);
        tvCare = (TextView) view.findViewById(R.id.tv_header_activity_personmessage_care);
        tvfans = (TextView) view.findViewById(R.id.tv_header_activity_personmessage_fans);
        tvContent = (TextView) view.findViewById(R.id.tv_header_activity_personmessage_content);

        initHeadData();

        listView.setOnItemClickListener(this);
    }

    private void initHeadData() {
        personMessageBean = new PersonMessageBean();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String url = "https://api.gotokeep.com/v1.1/people/" + id;
        GsonRequest<PersonMessageBean> gsonRequest = new GsonRequest<PersonMessageBean>(url, PersonMessageBean.class, new Response.Listener<PersonMessageBean>() {
            @Override
            public void onResponse(PersonMessageBean response) {
                personMessageBean = response;
                tvTitle.setText(response.getData().getUsername());
                tvDynamic.setText(response.getData().getStatistics().getTotalEntries() + "");
                tvCare.setText(response.getData().getFollowings() + "");
                tvfans.setText(response.getData().getFollowers() + "");
                tvContent.setText(response.getData().getBio());
                if (response.getData().getAvatar().equals("")) {
                    imageheader.setImageResource(R.mipmap.person_45_45);
                } else {
                    ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageheader,R.mipmap.person_45_45,R.mipmap.person_45_45);
                    VolleySingleton.getImageLoader().get(response.getData().getAvatar(),listener);
                }

                initData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

    private void initData() {
        personDynamicBase = new PersonDynamicBase();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String url = " https://api.gotokeep.com/v1.1/people/" + id + "/timeline";
        GsonRequest<PersonDynamicBase> gsonRequest = new GsonRequest<PersonDynamicBase>(url, PersonDynamicBase.class, new Response.Listener<PersonDynamicBase>() {
            @Override
            public void onResponse(PersonDynamicBase response) {
                personDynamicBase = response;
                adapter = new PersonDynamicListViewAdapter(PersonMessageActivity.this,response,personMessageBean.getData().getUsername(),personMessageBean.getData().getAvatar());
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, DynamicDetailActivity.class);
        intent.putExtra("id",personDynamicBase.getData().get(position - 1).get_id());
        startActivity(intent);
    }

    //menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity_personmessage,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_show_activity_personmessage :
                Intent intent = new Intent(this,PersonShowActivity.class);
                intent.putExtra("id",personMessageBean.getData().get_id());
                startActivity(intent);
                break;
        }
        return true;
    }
}
